﻿using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Customer;
using Common.Contract.Customer.Request.CustomerManager;
using Common.Contract.Customer.Response.CustomerManager;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Customer360AutoJob.Core.Configuration;
using Customer360AutoJob.Models.ViewModels.ExportDataToSmartSME;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;

namespace Customer360AutoJob.Jobs
{
    public class ExportCustomerNeedUpdateInfo : JobBase, IJob
    {
        #region Properties
        public UserRecord CurrentUserRecord { get; set; }

        #endregion
        public void Execute(IJobExecutionContext context)
        {
            CurrentUserRecord = new UserRecord
            {
                UserName = "admin",
                GroupIdentifier = "1"
            };
            var listCustomer = RetrieveListCustomer();
            DataTable dt = new DataTable();
            dt = ToDataTable(listCustomer);
            var fileName = "TableCustomerNeedUpdateInfoMBD" + DateTime.Now.ToString("dd/MM/yyyy") + ".xlsx";

            fileName = fileName.Replace("/", "");
            ExportToExcel(dt, fileName);
            Thread.Sleep(100);

            var listCustomerInfo = RetrieveListCustomerInfo();
            DataTable dtInfo = new DataTable();
            dtInfo = ToDataTable(listCustomerInfo);
            var fileNameInfo = "TableCustomerNeedUpdateInfoData" + DateTime.Now.ToString("dd/MM/yyyy") + ".xlsx";

            fileNameInfo = fileNameInfo.Replace("/", "");
            ExportToExcel(dtInfo, fileNameInfo);
            Thread.Sleep(100);
        }

        private List<CustNeedUpdateInfoViewModel> RetrieveListCustomer()
        {
            List<CustNeedUpdateInfoViewModel> result = new List<CustNeedUpdateInfoViewModel>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                filter.IsExport = true;
                RetrieveCustomerNeedToUpdateRequest request = new RetrieveCustomerNeedToUpdateRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustPriorityData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerNeedToUpdateResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerNeedToUpdate(request);
                result = (customerRecords == null || customerRecords.Records == null) ? new List<CustNeedUpdateInfoViewModel>() : customerRecords.Records.Select(x => new CustNeedUpdateInfoViewModel
                {
                    CIF = x.CIF,
                    NO_INFO_NEED_UPDATE = x.NoOfInfoNeedToUpdate
                }).ToList();
            }
            return result;
        }
        private List<CustNeedUpdateInfoDataViewModel> RetrieveListCustomerInfo()
        {
            List<CustNeedUpdateInfoDataViewModel> result = new List<CustNeedUpdateInfoDataViewModel>();
            using (ServiceClient<ICustomer> serviceClient = new ServiceClient<ICustomer>())
            {
                FilterRecord filter = new FilterRecord();
                filter.IsExport = true;
                RetrieveCustomerNeedUpdateInfoRawRequest request = new RetrieveCustomerNeedUpdateInfoRawRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.CustomerManagerController.CustPriorityData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 0,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                var customerRecords = new RetrieveCustomerNeedUpdateInfoRawResponse();
                customerRecords = serviceClient.Proxy.RetrieveCustomerNeedUpdateInfoRaw(request);
                result = (customerRecords == null || customerRecords.Records == null) ? new List<CustNeedUpdateInfoDataViewModel>() : customerRecords.Records.Select(x => new CustNeedUpdateInfoDataViewModel
                {
                    CIF = x.Cif,
                    INFO_CODE = x.InfoCode,
                    VALUE_INFO = x.ValueInfo
                }).ToList();
            }
            return result;
        }
    }
}